package com.syy.statisticalresults;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Grade implements WritableComparable<Grade> {
    private Integer rank;
    private String name;
    private Integer chinese;
    private Integer math;
    private Integer english;
    private Integer computer;
    private Double sum;
    private Double avg;

    public Grade() {
    }

    public Integer getRank() {
        return rank;
    }

    public void setRank(Integer rank) {
        this.rank = rank;
    }

    public Double getAvg() {
        return avg;
    }

    public void setAvg(Double avg) {
        this.avg = avg;
    }

    public Grade(String name, Integer chinese, Integer math, Integer english, Integer computer) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
        this.computer = computer;
        this.sum = (double) (chinese + math + english + computer);
        this.avg = this.sum / 4;
    }

    public Double getSum() {
        return sum;
    }

    public void setSum(Double sum) {
        this.sum = sum;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getChinese() {
        return chinese;
    }

    public void setChinese(Integer chinese) {
        this.chinese = chinese;
    }

    public Integer getMath() {
        return math;
    }

    public void setMath(Integer math) {
        this.math = math;
    }

    public Integer getEnglish() {
        return english;
    }

    public void setEnglish(Integer english) {
        this.english = english;
    }

    public Integer getComputer() {
        return computer;
    }

    public void setComputer(Integer computer) {
        this.computer = computer;
    }

    @Override
    public String toString() {
        return rank + "\t" + name + "\t" + chinese + "\t" +  math + "\t" +  english + "\t" +  computer + "\t" + sum + "\t" + avg;
    }

    @Override
    public int compareTo(Grade o) {
        return o.getMath().compareTo(this.getMath());
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(name);
        dataOutput.writeInt(chinese);
        dataOutput.writeInt(math);
        dataOutput.writeInt(english);
        dataOutput.writeInt(computer);
        dataOutput.writeDouble(sum);
        dataOutput.writeDouble(avg);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.name = dataInput.readUTF();
        this.chinese = dataInput.readInt();
        this.math = dataInput.readInt();
        this.english = dataInput.readInt();
        this.computer = dataInput.readInt();
        this.sum = dataInput.readDouble();
        this.avg = dataInput.readDouble();
    }
}
